package com.syj.jsxjj.ui.my.activity;

import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.syj.jsxjj.R;
import com.syj.jsxjj.app.App;
import com.syj.jsxjj.base.BaseActivity;
import com.syj.jsxjj.http.HttpManager;
import com.syj.jsxjj.http.HttpSubscriber;
import com.syj.jsxjj.ui.my.bean.MyBankBean;
import com.syj.jsxjj.ui.renzheng.bean.ContentBean;
import com.syj.jsxjj.ui.renzheng.bean.PersonalBean;
import com.syj.jsxjj.ui.renzheng.fragment.AuthenFragment;
import com.syj.jsxjj.util.FormatUtil;
import com.syj.jsxjj.util.StringUtil;
import com.syj.jsxjj.util.ToastUtil;
import com.syj.jsxjj.widget.ClearEditText;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by gym on 2017/7/14 0014.
 * 描述：
 */

public class MyBankActivity extends BaseActivity {

    @BindView(R.id.et_name)
    ClearEditText mName;
    @BindView(R.id.tv_bank)
    TextView mBank;
    @BindView(R.id.et_bank_number)
    ClearEditText mBankNumber;
    @BindView(R.id.et_bank_phone)
    ClearEditText mBankPhone;

    private OptionsPickerView pv;
    private ArrayList<String> professionalList = new ArrayList<>();
    private ContentBean contentBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_bank_layout;
    }


    @Override
    public void initPresenter() {
        contentBean = getIntent().getParcelableExtra(AuthenFragment.CONTENT_BEAN);
        if (contentBean!=null&&contentBean.getBankcard()!=null){
            mName.setText(contentBean.getBankcard().getName());
            mBank.setText(contentBean.getBankcard().getBank_card_name());
            mBankNumber.setText(contentBean.getBankcard().getBank_card_number());
            mBankPhone.setText(contentBean.getBankcard().getBank_card_phone());
        }
        initView();
    }

    @Override
    public void loadData() {

    }

    private void initView() {
        mTitle.setTitle(true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        }, R.string.personal_information);
        mTitle.setRightTitle("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
    }


    @OnClick({R.id.layout_choose_bank})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_choose_bank:
                if (pv == null) {
                    professionalList.add("中国银行");
                    professionalList.add("中国建设银行");
                    professionalList.add("中国农业银行");
                    professionalList.add("中国工商银行");
                    professionalList.add("中国交通银行");
                    professionalList.add("中国招商银行");
                    professionalList.add("中国中信银行");
                    professionalList.add("中国光大银行");
                    professionalList.add("浦发银行");
                    professionalList.add("平安银行");
                    professionalList.add("民生银行");
                    professionalList.add("上海银行");
                    pv = new OptionsPickerView(mContext);
                    pv.setPicker(professionalList);
                    pv.setCyclic(false);
                    pv.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
                        @Override
                        public void onOptionsSelect(int options1, int option2, int options3) {
                            mBank.setText(professionalList.get(options1));
                        }
                    });
                }
                pv.show();
                break;
        }
    }


    /**
     * 个人信息保存
     **/
    private void save() {
        if (check()) {
            MyBankBean bankBean = new MyBankBean();
            bankBean.setName(mName.getText().toString());
            bankBean.setBank_card_name(mBank.getText().toString());
            bankBean.setBank_card_number(mBankNumber.getText().toString());
            bankBean.setBank_card_phone(mBankPhone.getText().toString());
            contentBean.setBankcard(bankBean);
            toSubscribe(HttpManager.getApi().saveCertification("3",new Gson().toJson(contentBean)), new HttpSubscriber() {
                @Override
                protected void _onStart() {

                }

                @Override
                protected void _onNext(Object o) {
                    ToastUtil.showToast("保存成功");
                    finish();
                }

                @Override
                protected void _onError(String message) {

                }

                @Override
                protected void _onCompleted() {

                }
            });
//            PersonalBean personalBean = new PersonalBean();
//            personalBean.setName(mEtCardName.getText().toString());
//            personalBean.setId_card(mEtCardNumber.getText().toString());
//            personalBean.setWork(mTvDegree.getText().toString());
//            contentBean.setPersonal(personalBean);
//            toSubscribe(HttpManager.getApi().saveCertification2("1",new Gson().toJson(contentBean)), new HttpSubscriber() {
//                @Override
//                protected void _onStart() {
//                    App.loadingDefault(mActivity);
//                }
//
//                @Override
//                protected void _onNext(Object o) {
//                    ToastUtil.showToast("保存成功");
//                    finish();
//                }
//
//                @Override
//                protected void _onError(String message) {
//                    ToastUtil.showToast(message);
//                }
//
//                @Override
//                protected void _onCompleted() {
//                    App.hideLoading();
//                }
//            });
        }
    }

    private boolean check() {
        if (StringUtil.isBlankEdit(mName)) {
            ToastUtil.showToast("请输入真实姓名");
        } else if (StringUtil.isBlankEdit(mBank)) {
            ToastUtil.showToast("请选择银行");
        } else if (StringUtil.isBlankEdit(mBankNumber)) {
            ToastUtil.showToast("请输入银行卡号");
        } else if (StringUtil.isBlankEdit(mBankPhone)) {
            ToastUtil.showToast("请输入银行预留号码");
        } else {
            return true;
        }
        return false;
    }
}
